package model

// 角色权限关联表
type RoleAuthority struct {
	RoleId        string `json:"roleId" bson:"roleId"`               // 角色id
	AuthorityName string `json:"authorityName" bson:"authorityName"` // 权限名称
	Type          string `json:"type" bson:"type"`                   // 权限类型， API, MENU
	FormId        string `json:"formId" bson:"formId"`               // 表单id，设置表单权限
}

// 列表
type RoleAuthorities []*RoleAuthority

// 两种类型的权限
type RoleAuthoritiesSlice struct {
	Api  []*RoleAuthority
	Menu []*RoleAuthority
}

// 将两种类型的权限分开
func (ras *RoleAuthorities) SeparateWithType() *RoleAuthoritiesSlice {
	raSlice := &RoleAuthoritiesSlice{}
	for _, ra := range *ras {
		if ra.Type == "API" {
			raSlice.Api = append(raSlice.Api, ra)
		} else if ra.Type == "MENU" {
			raSlice.Menu = append(raSlice.Menu, ra)
		}
	}
	return raSlice
}

func (ras *RoleAuthorities) GenerateAuths() *Auths {
	auths := &Auths{}
	for _, ra := range *ras {
		if ra.Type == "API" {
			auths.ApiAuths = append(auths.ApiAuths, ra.AuthorityName)
		} else if ra.Type == "MENU" {
			auths.MenuAuths = append(auths.MenuAuths, ra.AuthorityName)
		}
	}
	return auths
}

func (ras *RoleAuthorities) GetAuths() []string {
	var auths []string
	for _, ra := range *ras {
		if ra.FormId != "" {
			continue
		}
		auths = append(auths, ra.AuthorityName)
	}
	return auths
}

func (ras *RoleAuthorities) GetFormAuths() map[string][]string {
	formAuth := make(map[string][]string)
	for _, ra := range *ras {
		if ra.FormId == "" {
			continue
		}
		formAuth[ra.FormId] = append(formAuth[ra.FormId], ra.AuthorityName)
	}
	return formAuth
}
